
export function flagToTree<T extends { id: number; pid: number }>(
  data: T[],
  id = 0,
): T[] {
  const res = data.filter((item: T) => item.pid === id);
  if (res.length === 0) return []; // 终止条件

  return res.map((child: T) => {
    const children = flagToTree(data, child.id);
    return {
      ...child,
      ...(children.length > 0 && { children }),
    };
  });
}
